/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.cspoker.server.rmi.export;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

import org.apache.log4j.Logger;
import org.cspoker.common.api.account.context.ForwardingRemoteAccountContext;
import org.cspoker.common.api.account.context.RemoteAccountContext;
import org.cspoker.common.api.cashier.context.ForwardingRemoteCashierContext;
import org.cspoker.common.api.cashier.context.RemoteCashierContext;
import org.cspoker.common.api.chat.context.ForwardingRemoteChatContext;
import org.cspoker.common.api.chat.context.RemoteChatContext;
import org.cspoker.common.api.chat.listener.ChatListener;
import org.cspoker.common.api.chat.listener.RemoteChatListener;
import org.cspoker.common.api.lobby.context.ExternalRemoteLobbyContext;
import org.cspoker.common.api.lobby.listener.LobbyListener;
import org.cspoker.common.api.lobby.listener.RemoteLobbyListener;
import org.cspoker.common.api.shared.context.ExternalServerContext;
import org.cspoker.common.api.shared.context.ForwardingExternalRemoteServerContext;
import org.cspoker.common.api.shared.context.ServerContextConverter;
import org.cspoker.common.api.shared.exception.IllegalActionException;
import org.cspoker.common.elements.table.TableId;

public class ExportingServerContext extends ForwardingExternalRemoteServerContext {

	private final static Logger logger = Logger.getLogger(ExportingServerContext.class);

	public ExportingServerContext(ExternalServerContext serverContext){
		super(new ServerContextConverter(serverContext));
	}

	@Override
	public RemoteAccountContext getAccountContext() throws RemoteException {
		try {
			ForwardingRemoteAccountContext wrappedContext = new ForwardingRemoteAccountContext(super.getAccountContext());
			return (RemoteAccountContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	@Override
	public RemoteCashierContext getCashierContext() throws RemoteException {
		try {
			RemoteCashierContext wrappedContext = new ForwardingRemoteCashierContext(super.getCashierContext());
			return (RemoteCashierContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	@Override
	public RemoteChatContext getServerChatContext(ChatListener chatListener)
	throws RemoteException, IllegalActionException {		
		try {
			RemoteChatContext wrappedContext = new ForwardingRemoteChatContext(super.getServerChatContext(chatListener));
			return (RemoteChatContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	@Override
	public RemoteChatContext getServerChatContext(RemoteChatListener chatListener)
	throws RemoteException, IllegalActionException {
		try {
			RemoteChatContext wrappedContext = new ForwardingRemoteChatContext(super.getServerChatContext(chatListener));
			return (RemoteChatContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}
	
	@Override
	public RemoteChatContext getTableChatContext(ChatListener chatListener,
			TableId tableId) throws RemoteException, IllegalActionException {
		try {
			RemoteChatContext wrappedContext = new ForwardingRemoteChatContext(super.getTableChatContext(chatListener, tableId));
			return (RemoteChatContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}
	
	@Override
	public RemoteChatContext getTableChatContext(
			RemoteChatListener chatListener, TableId tableId)
			throws RemoteException {
		try {
			RemoteChatContext wrappedContext = new ForwardingRemoteChatContext(super.getTableChatContext(chatListener, tableId));
			return (RemoteChatContext) UnicastRemoteObject.exportObject(wrappedContext, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	@Override
	public ExternalRemoteLobbyContext getLobbyContext(LobbyListener lobbyListener)
	throws RemoteException, IllegalActionException {
		try {
			ExternalRemoteLobbyContext remoteObject = new ExportingLobbyContext(super.getLobbyContext(lobbyListener));
			return (ExternalRemoteLobbyContext) UnicastRemoteObject.exportObject(remoteObject, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}

	@Override
	public ExternalRemoteLobbyContext getLobbyContext(RemoteLobbyListener lobbyListener)
	throws RemoteException, IllegalActionException {
		try {
			ExternalRemoteLobbyContext remoteObject = new ExportingLobbyContext(super.getLobbyContext(lobbyListener));
			return (ExternalRemoteLobbyContext) UnicastRemoteObject.exportObject(remoteObject, 0);
		} catch (RemoteException exception) {
			logger.error(exception.getMessage(), exception);
			throw exception;
		}
	}


}
